package com.wordpress.javagameengine.lazarek.dominik;

import javax.swing.*;
import java.awt.*;

/**
 * this class is the window, in which the game will run in. 
 * for default, the window will paint a blue background
 * @author dommme
 *
 */
public class GamePanel extends JPanel
{

	private static final long serialVersionUID = 4096281521006562709L;
	private JFrame frame;
	private GameControl gameControl;

	long delta = 0;
	long last = 0;  //some variables to calculate the framerate
	long fps = 0;
	
	/**
	 * the constructor initializes the Window in the specified size and position on the screen
	 * @param x x-coordinate on the screen where the Window will be painted
	 * @param y y-coordinate on the screen where the Window will be painted
	 * @param width the width of the window in pixel
	 * @param height the height of the window in pixel
	 * @param windowTitle the title of the new Window
	 * @param gameControl a reference to the gameControl to be able to call gameControl.draw(Graphics g) in the 
	 * paintComponent() method
	 */
	public GamePanel(int x, int y ,int width, int height, String windowTitle, GameControl _gameControl)
	{
		this.setPreferredSize(new Dimension(width, height));
		this.frame = new JFrame(windowTitle);
		this.gameControl = _gameControl;
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocation(x, y);
		frame.add(this);
		frame.pack();
		frame.setVisible(true);
		frame.setResizable(false);
		frame.setBackground(Color.blue);
	}
	
	/**
	 * this function calculates the current framerate of the game.
	 * @return the Framerate in frames/second
	 */
	public long calculateFramerate()
	{
		this.delta = System.nanoTime() - this.last;
		this.last = System.nanoTime();
		return (long)(1e9) / delta;
	}
	
	@Override
	public void paintComponents(Graphics g) {
			super.paintComponents(g);
			
			this.gameControl.draw(g);
	}
}// end class

